<template>
  <div :key="Math.random()" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts' // echarts theme
import { debounce } from '@/utils'

require('echarts/theme/macarons')

export default {
  props: {
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    chartData: {
      type: Object,
      required: true
    }
    // predictedTrend: {
    //   type: String,
    //   required: true // 'up' or 'down'
    // }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
        this.initChart()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ name, data }) {
      const xData = data.name
      const yData = data.value
      const lineData = data.lineData
      const flag = Number(lineData[lineData.length - 1]) > Number(lineData[lineData.length - 2])
      const yData1 = JSON.parse(JSON.stringify(yData))
      this.chart.setOption({
        grid: {
          left: '5%',
          right: '8%',
          bottom: '5%',
          top: '10%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          formatter: function(params) {
            let res = ''
            if (params[0].axisValue === data.name[data.name.length - 1] && (params[0].axisValue).substring(0, 4) === '2024') {
              res = `<div style="display: inline-block; width: 10px; height: 10px; border-radius: 50%; background-color: ${flag ? 'rgba(50,205,50, 1)' : 'rgba(220,20,60,1)'}; margin-right: 5px;"></div>${params[0].name} 预测指数: ${data.lineData[data.name.length - 1]}`
            } else {
              res = `${params[0].name} <br/>
                <div style="display: inline-block; width: 10px; height: 10px; border-radius: 50%; background-color: #36C4FF; margin-right: 5px;"></div>活力指数:  ${params[0].data} <br/>
                <div style="display: inline-block; width: 10px; height: 10px; border-radius: 50%; background-color:#FFC273; margin-right: 5px;"></div>预测指数:  ${params[1].value} <br/>`
            }
            return res
          },
          axisPointer: {
            type: 'shadow',
            shadowStyle: {
              color: 'rgba(0, 154, 255, 0.2)'
            }
          },
          show: true
        },
        // symbol: function (value, series) {
        //   console.log(999,value, series);
        //   if (
        //     series.dataIndex >= that.beforeLastYearSymbolStart &&
        //     series.dataIndex <= that.beforeLastYearSymbolEnd
        //   ) {
        //     return "emptyCircle"; // 拐点显示为圆形
        //   } else {
        //     return "none"; // 其他拐点不显示
        //   }
        // },
        animation: false,
        legend: {
          data: ['活力指数', '预测指数'],
          itemGap: 24, // 修改间距
          show: false,
          textStyle: {
            fontSize: 10,
            color: 'skyblue',
            padding: [0, 0, 0, 0] // 修改文字和图标距离
          }
        },
        xAxis: [{
          type: 'category',
          data: xData,
          axisTick: {
            alignWithLabel: true
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: '#36C4FF',
              fontSize: this.echartsToRem(12)
            },
            margin: this.echartsToRem(12)
          }
        }],
        yAxis: [{
          splitLine: {
            lineStyle: {
              type: 'solid',
              color: '#333'
            },
            show: false
          },
          interval: 30, // 步长
          min: 0, // 起始
          max: 210,
          splitArea: { show: false },
          axisLine: { show: false },
          axisTick: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: '#36C4FF',
              fontSize: this.echartsToRem(12),
              formatter: '{value} 亿'
            }
          }
        },
        {
          type: 'value',
          axisTick: {
            show: false
          },
          splitLine: {
            show: false
          },

          interval: 30, // 步长
          min: 0, // 起始
          max: 210,
          show: false,
          splitArea: { show: false },
          axisLine: { show: false },
          axisLabel: {
            formatter: '{value} ',
            textStyle: {
              color: '#36C4FF',
              fontSize: this.echartsToRem(12)
            }
          }
        }],
        series: [
          {
            name: '活力指数',
            type: 'bar',
            barWidth: this.echartsToRem(20),
            itemStyle: {
              normal: {
                barBorderRadius: [10, 10, 0, 0],
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1, [{
                    offset: 0,
                    color: 'rgba(101, 239, 255, 1)'
                  },
                  {
                    offset: 1,
                    color: 'rgba(0, 154, 255, 0)'
                  }
                  ]
                )
              }
            },
            data: data.valueTime !== '2024' ? yData1 : yData1.slice(0, -1).concat(undefined)
          },
          {
            name: '预测指数',
            type: 'line',
            yAxisIndex: 1,
            smooth: false,
            zlevel: 4,
            symbolSize: 2,
            color: '#FFC273',
            lineStyle: {
              normal: {
                width: 2,
                color: '#FFC273',
                type: 'solid'
              }
            },
            data: lineData
          },
          {
            type: 'line',
            yAxisIndex: 1,
            zlevel: 3,
            itemStyle: {
              normal: {
                color: '#bcbcbc',
                label: {
                  show: false,
                  textStyle: {
                    color: '#6E6E6E',
                    fontWeight: 'bold'
                  }
                }
              }
            },
            lineStyle: {
              normal: {
                width: 2,
                color: lineData[lineData.length - 1] > lineData[lineData.length - 2] ? 'green' : 'red',
                type: 'dashed'
              }
            },
            symbol: 'none',
            data: [...new Array(lineData.length - 2).fill('undefined'), lineData[lineData.length - 2], lineData[lineData.length - 1]]
          },
          {
            name: 'TX1',
            type: 'effectScatter',
            data: [...new Array(lineData.length - 1).fill('undefined'), lineData[lineData.length - 1]],
            itemStyle: {
              color: flag ? 'rgba(50,205,50, 1)' : 'rgba(220,20,60,1)'
            },
            yAxisIndex: 1,
            symbol: 'circle',
            symbolSize: 5,
            showEffectOn: 'render',
            rippleEffect: {
              color: flag ? 'rgba(50,205,50, 1)' : 'rgba(220,20,60,1)',
              period: 3,
              scale: 5,
              brushType: 'fill'
            },
            zlevel: 2
          }
        ]
      }, true)
    }
  }
}
</script>
